﻿import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.jfree.ui.RefineryUtilities;

public class MainClass extends JFrame
{
	public static final int WIDTH = 600;				//畫面寬度
	public static final int HEIGHT = 600;				//畫面高度
	
	//private static JTextField TF_BornDate;
	/**
	 * Hw3 Login Testing 
	 * 所有使用者資料紀錄在UserSet.txt中, 請先建置
	 */	
	
	private String user ;
	private JMenuBar chartMenuBarOuter;
	private JMenuBar seeRecordMenuBar;
	private JMenuBar newRecordMenuBar;
	private JButton chooseStartDateBt;
	private JButton chooseEndDateBt;
	private JButton chooseDateBt;
	private JMenu FoodCategoryMenu;
	//private JMenu FoodMenu;
	private JTextArea numberArea;
	private JLabel unitLabel;
	private JMenu grainMenu;
	private JMenu eggMenu;
	private JMenu meatMenu;
	private JMenu oilMenu;
	private JMenu fruitMenu;
	private JTextArea recordArea;
	private JMenu modeMenu;
	private LoadData grainData;
	private LoadData meatData;	
	private LoadData oilData;
	private LoadData eggData;
	private LoadData fruitData;
	private Chart SpiderWebChart;
	
	
	MainClass( String user )
	{
		super( ) ;
		this.user = user ;
		initialMainFrame( ) ;

	}

	void initialMainFrame( )
	{
		setSize(WIDTH,HEIGHT);										//設定視窗大小
		setTitle( "Fat/Zero" ) ;									//設定標題
		setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE ) ;			//設定"隱藏"
		setResizable( false ) ;										//不能改變視窗大小
		Dimension dim = getToolkit().getScreenSize(); 				//取得螢幕大小
		setLocation(dim.width/2-getWidth()/2, dim.height/2 - getHeight()/2);
		
		
		
		setLayout( new BorderLayout()) ;							//東西南北中布局
		
		
		/**呼叫食物資料**/
		//JButton FoodData = new JButton( "讀取食物資料" ) ;
		/*Meat Grain Oil Fruit Egg*/
		
		grainData = new LoadData( 0 ,"Grain" ) ;
		meatData  = new LoadData( 2 , "Meat" ) ;	
		oilData   = new LoadData( 3 , "Oil" ) ;
		eggData   = new LoadData( 1 , "Egg" ) ;
		fruitData = new LoadData( 4 , "Fruit" ) ;

		/************/	
		
		
		
		//上方的menu
		JMenuBar mainMenuBar = new JMenuBar();
			JMenu functionMenu = new JMenu("功能");
				JMenuItem aboutMenuItem = new JMenuItem("關於");
				aboutMenuItem.addActionListener(new about_act());
				functionMenu.add(aboutMenuItem);
				
				JMenuItem entryBodyDataMenuItem = new JMenuItem("輸入身高體重基本資料");
				entryBodyDataMenuItem.addActionListener( new SetBodyData( user ) ) ;	
				functionMenu.add(entryBodyDataMenuItem);
				
				
	
			mainMenuBar.add(functionMenu);
			//mainMenuBar.add(recordMenu);
		
		//中間的區塊
			JPanel midPanel = new JPanel();
				midPanel.setBackground(Color.black);
				midPanel.setLayout(new BorderLayout());
				

					chartMenuBarOuter = new JMenuBar();
					chartMenuBarOuter.setBackground(new Color(238,238,238));
						modeMenu = new JMenu("↓查看紀錄↓");
							JMenuItem newRecordMode = new JMenuItem("↓新增紀錄↓");
								newRecordMode.addActionListener(new changeMode_act());
								//modeMenu.setBackground(Color.white);
								//modeMenu.setOpaque(true);
							JMenuItem seeRecordMode = new JMenuItem("↓查看紀錄↓");
								seeRecordMode.addActionListener(new changeMode_act());
							
						modeMenu.add(newRecordMode);
						modeMenu.add(seeRecordMode);
	
					//JPanel chartMenuBar = new JPanel();
					//chartMenuBar.setLayout(new GridLayout(1,5));
						seeRecordMenuBar = new JMenuBar();
						seeRecordMenuBar.setBackground(new Color(238, 238, 238));
						
							JLabel startTime = new JLabel("起始時間：");
							chooseStartDateBt = new JButton("2012/12/21");
								chooseStartDateBt.setBackground(Color.white);
								chooseStartDateBt.addActionListener(new chooseDateBt_act());
							JLabel endTime = new JLabel("結束時間：");
							chooseEndDateBt = new JButton("2012/12/21");
								chooseEndDateBt.addActionListener(new chooseDateBt_act());	
								chooseEndDateBt.setBackground(Color.white);
		
							JMenu typeMenu = new JMenu("類型");
								typeMenu.setBackground(Color.white);
								JMenuItem foodRecordMenuItem = new JMenuItem("食物");
								JMenuItem sportRecordMenuItem = new JMenuItem("運動");
								JMenuItem weightRecordMenuItem = new JMenuItem("體重");
			
								typeMenu.add(foodRecordMenuItem);
								typeMenu.add(sportRecordMenuItem);
								typeMenu.add(weightRecordMenuItem);
					
							seeRecordMenuBar.add(startTime);
							seeRecordMenuBar.add(chooseStartDateBt);
							seeRecordMenuBar.add(Box.createHorizontalStrut( 20 ));
							seeRecordMenuBar.add(endTime);
							seeRecordMenuBar.add(chooseEndDateBt);
							seeRecordMenuBar.add(Box.createHorizontalStrut( 20 ));
							seeRecordMenuBar.add(typeMenu);
					
							
							
						newRecordMenuBar = new JMenuBar();
							newRecordMenuBar.setBackground(new Color(238,238,238));
							chooseDateBt = new JButton("2012/12/21");
							chooseDateBt.setBackground(Color.white);
							chooseDateBt.addActionListener(new chooseDateBt_act());
							
							
							FoodCategoryMenu = new JMenu("↓五穀根莖類↓");
								JMenuItem grainMenuItem = new JMenuItem("↓五穀根莖類↓");
									grainMenuItem.addActionListener(new chooseCategory_act());
								JMenuItem eggMenuItem = new JMenuItem("↓蛋類↓");
									eggMenuItem.addActionListener(new chooseCategory_act());
								JMenuItem meatMenuItem = new JMenuItem("↓肉類↓");
									meatMenuItem.addActionListener(new chooseCategory_act());
								JMenuItem oilMenuItem = new JMenuItem("↓油脂類↓");
									oilMenuItem.addActionListener(new chooseCategory_act());
								JMenuItem fruitMenuItem = new JMenuItem("↓水果類↓");
									fruitMenuItem.addActionListener(new chooseCategory_act());
							
								FoodCategoryMenu.add(grainMenuItem);
								FoodCategoryMenu.add(eggMenuItem);
								FoodCategoryMenu.add(meatMenuItem);
								FoodCategoryMenu.add(oilMenuItem);
								FoodCategoryMenu.add(fruitMenuItem);
								
								
							
							grainMenu = new JMenu(grainData.AllKindFood.get( 0 ).Name);
								grainMenu.setName("0");
								JMenuItem grainItem;
								for( int i = 0 ; i < grainData.AllKindFood.size( ) ; i++ )
								{
									grainItem = new JMenuItem(grainData.AllKindFood.get( i ).Name); //ID,Kind,Name,Kcal 
									grainItem.setName(""+i);
									grainItem.addActionListener(new chooseFood_act());
									grainMenu.add(grainItem);
								}
								
							eggMenu = new JMenu(eggData.AllKindFood.get( 0 ).Name);	
								eggMenu.setName("0");
								JMenuItem eggItem;
								for( int i = 0 ; i < eggData.AllKindFood.size( ) ; i++ )
								{
									eggItem = new JMenuItem(eggData.AllKindFood.get( i ).Name);
									eggItem.setName(""+i);
									eggItem.addActionListener(new chooseFood_act());
									eggMenu.add(eggItem);
								}
								
							meatMenu = new JMenu(meatData.AllKindFood.get( 0 ).Name);	
								meatMenu.setName("0");
								JMenuItem meatItem;
								for( int i = 0 ; i < meatData.AllKindFood.size( ); i++ )
								{
									meatItem = new JMenuItem(meatData.AllKindFood.get( i ).Name);
									meatItem.setName(""+i);
									meatItem.addActionListener(new chooseFood_act());
									meatMenu.add(meatItem);
								}
							oilMenu = new JMenu(oilData.AllKindFood.get( 0 ).Name);
								oilMenu.setName("0");
								JMenuItem oilItem;
								for( int i = 0 ; i < oilData.AllKindFood.size( ) ; i++ )
								{
									oilItem = new JMenuItem(oilData.AllKindFood.get( i ).Name);
									oilItem.setName(""+i);
									oilItem.addActionListener(new chooseFood_act());
									oilMenu.add(oilItem);
								}
								
							fruitMenu = new JMenu(fruitData.AllKindFood.get( 0 ).Name);
								fruitMenu.setName("0");
								JMenuItem fruitItem;
								for( int i = 0 ; i < fruitData.AllKindFood.size( ) ; i++ )
								{
									fruitItem = new JMenuItem(fruitData.AllKindFood.get( i ).Name);
									fruitItem.setName(""+i);
									fruitItem.addActionListener(new chooseFood_act());
									fruitMenu.add(fruitItem);
								}

							MaxLengthTextDocument maxLength = new MaxLengthTextDocument( ) ;	// 最大長度
							maxLength.setMaxChars(4 ) ;

							numberArea = new JTextArea();		
							//numberArea.setText("0");
							numberArea.setDocument( maxLength ) ;
								
							/*
							numberMenu = new JMenu("↓0↓");
								JMenuItem numberMenuItem;
								for( int i = 0 ; i < 15; i++ )
								{
									numberMenuItem = new JMenuItem(""+i);
									numberMenu.add(numberMenuItem);
								}	
							*/
								
							unitLabel = new JLabel("碗");//五股類/ 碗 , 肉類 / 份, 油類 / 湯匙 , 水果,蛋類 / 顆
							
							JButton addBt = new JButton("add");
								addBt.addActionListener(new addBt_act());
								
							JButton clrBt = new JButton("clr");
								clrBt.addActionListener(new clr_act());
								
							JButton saveBt = new JButton("save");
								saveBt.addActionListener(new save_act());
								
							newRecordMenuBar.add(chooseDateBt);
							newRecordMenuBar.add(Box.createHorizontalStrut(10));
							newRecordMenuBar.add(FoodCategoryMenu);	
							newRecordMenuBar.add(Box.createHorizontalStrut(10));
							newRecordMenuBar.add(grainMenu);
							newRecordMenuBar.add(Box.createHorizontalStrut(10));
							newRecordMenuBar.add(numberArea);
							newRecordMenuBar.add(unitLabel);
							newRecordMenuBar.add(Box.createHorizontalStrut(5));
							newRecordMenuBar.add(addBt);
							newRecordMenuBar.add(Box.createHorizontalStrut(5));
							newRecordMenuBar.add(clrBt);
							newRecordMenuBar.add(Box.createHorizontalStrut(5));
							newRecordMenuBar.add(saveBt);
							
						chartMenuBarOuter.add(modeMenu);
						chartMenuBarOuter.add(Box.createHorizontalStrut( 20 ));
						chartMenuBarOuter.add(newRecordMenuBar);	

				SpiderWebChart = new Chart( "TestChart" ) ;
				SpiderWebChart.dispose();
				SpiderWebChart.pack( ) ;
		        RefineryUtilities.centerFrameOnScreen( SpiderWebChart ) ;
		        //SpiderWebChart.setVisible( true ) ;
		        JPanel jp8 = SpiderWebChart.createDemoPanel( ) ;
		        
		        
		        //midPanel.add(chartMenuBar,BorderLayout.NORTH);
		        midPanel.add(chartMenuBarOuter,BorderLayout.SOUTH);
		        midPanel.add(jp8,BorderLayout.CENTER);
	
		//下面的區塊
			JPanel buttomPanel = new JPanel();
				buttomPanel.setBackground(Color.white);
				buttomPanel.setLayout(new BorderLayout());
			
				recordArea = new JTextArea(7,0);
				recordArea.setEnabled(false);
				recordArea.setPreferredSize(new Dimension(0,50));  
			//	JMenuBar recordMenuBar = new 

				buttomPanel.add(recordArea,BorderLayout.CENTER);

		this.add(mainMenuBar,BorderLayout.NORTH);
		this.add(midPanel,BorderLayout.CENTER);
		this.add(buttomPanel,BorderLayout.SOUTH);

	}
	
	private class chooseDateBt_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			//DatePopup DP = new DatePopup(TF_BornDate);
			//DP.showDialog();
			DatePopup DP = new DatePopup();
			DP.showDialog();
			JButton tmpBt = (JButton)e.getSource();
			
			SpiderWebChart.SetZero( ) ;
			recordArea.setText("");
			if(!DP.getFinalDate().equals(""))
			{
				tmpBt.setText(DP.getFinalDate());
			}
		}
		
	}

	private class changeMode_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			
			JMenuItem tmpMenuItem = (JMenuItem)e.getSource();
			modeMenu.setText(tmpMenuItem.getText());

			if(tmpMenuItem.getText().equals("↓新增紀錄↓"))
			{
				
				chartMenuBarOuter.remove(seeRecordMenuBar);
				chartMenuBarOuter.add(newRecordMenuBar);
			}
			else if(tmpMenuItem.getText().equals("↓查看紀錄↓"))
			{
				chartMenuBarOuter.remove(newRecordMenuBar);
				chartMenuBarOuter.add(seeRecordMenuBar);
			}
			chartMenuBarOuter.repaint();
			
		}
		
	}

	private class chooseCategory_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			
			JMenuItem tmpMenuItem = (JMenuItem)e.getSource();
			FoodCategoryMenu.setText(tmpMenuItem.getText());
			
			newRecordMenuBar.remove(4);
			
			if(tmpMenuItem.getText().equals("↓五穀根莖類↓"))
			{
				newRecordMenuBar.add(grainMenu,4);
				unitLabel.setText("碗");
			}
			else if(tmpMenuItem.getText().equals("↓蛋類↓"))
			{
				newRecordMenuBar.add(eggMenu,4);
				unitLabel.setText("顆");
			}
			else if(tmpMenuItem.getText().equals("↓肉類↓"))
			{
				newRecordMenuBar.add(meatMenu,4);
				unitLabel.setText("份");
			}
			else if(tmpMenuItem.getText().equals("↓油脂類↓"))
			{
				newRecordMenuBar.add(oilMenu,4);
				unitLabel.setText("湯匙");
			}
			else if(tmpMenuItem.getText().equals("↓水果類↓"))
			{
				newRecordMenuBar.add(fruitMenu,4);
				unitLabel.setText("顆");
			}
			newRecordMenuBar.repaint();
		}
		
	}
	
	private class chooseFood_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			
			JMenuItem tmpMenuItem = (JMenuItem)e.getSource();
			
			JMenu tmpMenu = (JMenu)newRecordMenuBar.getComponent(4); 
			tmpMenu.setText(tmpMenuItem.getText());
			tmpMenu.setName(tmpMenuItem.getName());

		}
		
	}
	
	private class about_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			
			JOptionPane.showMessageDialog( null , "就是一群不太會寫程式的人\n初次使用GIT HUB，肯求各位大大無私的給分，\n我們最愛莊教授了*>_0*" ) ;

		}
		
	}
	
	private class clr_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			recordArea.setText("");
			SpiderWebChart.SetZero( ) ;
		}
		
	}
	
	private class save_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			recordArea.insert("資料已存\n",0);
			SpiderWebChart.ModifyDataDay();
		}
		
	}
	
	private class addBt_act implements ActionListener
	{
		@Override		
		public void actionPerformed(ActionEvent e) 
		{
			
			//JMenuItem tmpMenuItem = (JMenuItem)e.getSource();
			
			JMenu tmpMenu1 = (JMenu)newRecordMenuBar.getComponent(2);
			JMenu tmpMenu2 = (JMenu)newRecordMenuBar.getComponent(4);
			
			
			int categoryID = 0;
			double calories = 0;
			
			
			if(tmpMenu1.getText().equals("↓五穀根莖類↓"))
			{
				categoryID = 0;
				calories = grainData.AllKindFood.get(Integer.parseInt(tmpMenu2.getName())).Kcal;  //ID,Kind,Name,Kcal 
			}
			else if(tmpMenu1.getText().equals("↓蛋類↓"))
			{
				categoryID = 1;
				calories = eggData.AllKindFood.get(Integer.parseInt(tmpMenu2.getName())).Kcal;  //ID,Kind,Name,Kcal 
			}
			else if(tmpMenu1.getText().equals("↓肉類↓"))
			{
				categoryID = 2;
				calories = meatData.AllKindFood.get(Integer.parseInt(tmpMenu2.getName())).Kcal;  //ID,Kind,Name,Kcal 
			}
			else if(tmpMenu1.getText().equals("↓油脂類↓"))
			{
				categoryID = 3;
				calories = oilData.AllKindFood.get(Integer.parseInt(tmpMenu2.getName())).Kcal;  //ID,Kind,Name,Kcal 
			}
			else if(tmpMenu1.getText().equals("↓水果類↓"))
			{
				categoryID = 4;
				calories = fruitData.AllKindFood.get(Integer.parseInt(tmpMenu2.getName())).Kcal;  //ID,Kind,Name,Kcal 
			}
			
			calories *= Integer.parseInt(numberArea.getText());
			SpiderWebChart.ModifyData(categoryID , calories ) ;

			recordArea.insert(chooseDateBt.getText()+" "+tmpMenu1.getText()+"中的"+tmpMenu2.getText()+numberArea.getText()+unitLabel.getText()+"\n",0);

		}
		
	}
	
	public static void main( String[] args )
	{		

		LoginFrame lf = new LoginFrame( ) ;
		lf.setVisible(true);
			
	}
}
